<include file="public/layout" />
<style>
    dd.opt table{
        width: 100%;
    }
    dd.opt tr{
        border: 1px solid #f4f4f4;
        padding: 8px;
    }
    dd.opt tr td{
        border: 1px solid #f4f4f4;
    }
    .ys-btn-close {
        position: relative;
        top: -12px;
        left: -16px;
        width: 18px;
        height: 18px;
        border: 1px solid #ccc;
        line-height: 18px;
        text-align: center;
        display: inline-block;
        border-radius: 50%;
        z-index: 1;
        background-color: #fff;
        cursor: pointer;
    }
    .selected-group-goods {
        background-color: #FFF;
        width: 162px;
        padding: 9px;
        margin-bottom: 10px;
        border: solid 1px #E6E6E6;
        box-shadow: 2px 2px 0 rgba(153,153,153,0.1);
    }

</style>
<load href="__ROOT__/public/plugins/Ueditor/ueditor.config.js"/>
<load href="__ROOT__/public/plugins/Ueditor/ueditor.all.min.js"/>
<script type="text/javascript" charset="utf-8" src="__ROOT__/public/plugins/Ueditor/lang/zh-cn/zh-cn.js"></script>
<script src="__ROOT__/public/static/js/layer/laydate/laydate.js"></script>
<style type="text/css">
    html, body {
        overflow: visible;
    }
</style>

<body style="background-color: #FFF; overflow: auto;">
<div id="toolTipLayer" style="position: absolute; z-index: 9999; display: none; visibility: visible; left: 95px; top: 573px;"></div>
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
<div class="page">
    <div class="fixed-bar">
        <div class="item-title"><a class="back" href="javascript:history.back();" title="返回列表"><i class="fa fa-arrow-circle-o-left"></i></a>
            <div class="subject">
                <h3>优惠促销管理 - 添加与编辑商品促销活动</h3>
                <h5>网站系统编辑商品促销活动</h5>
            </div>
        </div>
    </div>
    <form class="form-horizontal" id="handleposition" method="post">
        <input type="hidden" name="id" value="{$info.id}">
        <input type="hidden" id="coupon_count" value="{$coupon_list|count}">
        <div class="ncap-form-default">
            <dl class="row">
                <dt class="tit">
                    <label><em>*</em>促销活动名称</label>
                </dt>
                <dd class="opt">
                    <input type="text" name="title" id="title" value="{$info.title}" class="input-txt">
                    <p class="notic">促销活动名称</p>
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label><em>*</em>促销活动类型</label>
                </dt>
                <dd class="opt">
                    <select id="prom_type" name="type">
                        <option value="0" <if condition="$info[type] eq 0">selected</if>>直接打折</option>
                        <option value="1" <if condition="$info[type] eq 1">selected</if>>减价优惠</option>
                        <option value="2" <if condition="$info[type] eq 2">selected</if>>固定金额出售</option>
                        <option value="3" <if condition="$info[type] eq 3">selected</if>>买就赠代金券</option>
                    </select>
                    <p class="notic">促销活动类型</p>
                </dd>
            </dl>
            <dl class="row" id="expression">
                <dt class="tit">
                    <label><em>*</em>折扣</label>
                </dt>
                <dd class="opt">
                    <input type="text" name="expression"  value="{$info.expression}" class="input-txt">
                    <p class="notic">% 折扣值(1-100 如果打9折，请输入90)</p>
                </dd>
            </dl>
            <dl class="row" id="buy_limit">
                <dt class="tit">
                    <label><em>*</em>限购数量</label>
                </dt>
                <dd class="opt">
                    <input name="buy_limit" value="{$info.buy_limit}" type="text" class="input-txt" onkeyup="this.value=this.value.replace(/[^\d]/g,'')"/>
                    <span class="err" id="err_buy_limit"></span>
                    <p class="notic">限购数量</p>
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label><em>*</em>开始时间</label>
                </dt>
                <dd class="opt">
                    <input type="text" id="start_time" name="start_time" value="{:date('Y-m-d H:i:s', $info['start_time'])}"  class="input-txt">
                    <p class="notic">优惠开始时间</p>
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label><em>*</em>结束时间</label>
                </dt>
                <dd class="opt">
                    <input type="text" id="end_time" name="end_time" value="{:date('Y-m-d H:i:s', $info['end_time'])}" class="input-txt">
                    <p class="notic">优惠结束时间</p>
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label><em>*</em>宣传图片</label>
                </dt>
                <dd class="opt">
                    <div class="input-file-show">
                        <span class="show">
                            <a id="img_a" target="_blank" class="nyroModal" rel="gal" href="{$info.prom_img}">
                                <i id="img_i" class="fa fa-picture-o" onmouseover="layer.tips('<img src={$info.prom_img}>',this,{tips: [1, '#fff']});" onmouseout="layer.closeAll();"></i>
                            </a>
                        </span>
           	            <span class="type-file-box">
                            <input type="text" id="prom_img" name="prom_img" value="{$info.prom_img}" class="type-file-text">
                            <input type="button" name="button" id="button1" value="选择上传..." class="type-file-button">
                            <input class="type-file-file" onClick="GetUploadify(1,'','activity','img_call_back')" size="30" hidefocus="true" nc_type="change_site_logo" title="点击前方预览图可查看大图，点击按钮选择文件并提交表单后上传生效">
                        </span>
                    </div>
                    <span class="err"></span>
                    <p class="notic">请上传图片格式文件</p>
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label><em>*</em>选择优惠商品</label>
                </dt>
                <dd class="opt">
                    <div style="overflow: hidden;" id="selected_group_goods">
                        <volist name="prom_goods" id="goods">
                            <empty name="goods[SpecGoodsPrice]">
                                <div style="float: left;margin-right: 20px">
                                    <input type="hidden" name="goods[{$goods.goods_id}][goods_id]" value="{$goods.goods_id}"/>
                                    <input type="hidden" name="store_count[]" value="{$goods.store_count}"/>
                                    <div class="ys-btn-close" style="top: 15px;left: 172px;">×</div>
                                    <div class="selected-group-goods">
                                        <div class="goods-thumb"><img style="width: 162px;height: 162px" src="{$goods['goods_id']|goods_thum_images=162,162}"/></div>
                                        <div class="goods-name">
                                            <a target="_blank" href="{:U('Home/Goods/goodsInfo',array('id'=>$goods['goods_id']))}">{$goods.goods_name}</a>
                                        </div>
                                        <div class="goods-price">商城价：￥{$goods.shop_price}库存:{$goods.store_count}</div>
                                    </div>
                                </div>
                                <else/>
                                <volist name="goods[SpecGoodsPrice]" id="spec">
                                    <if condition="$spec.prom_type eq 3 AND $spec.prom_id eq $info.id">
                                        <div style="float: left;margin-right: 20px">
                                            <input type="hidden" name="goods[{$goods.goods_id}][goods_id]" value="{$goods.goods_id}"/>
                                            <input type="hidden" name="goods[{$goods.goods_id}][item_id][]" value="{$spec.item_id}"/>
                                            <input type="hidden" name="store_count[]" value="{$spec.store_count}"/>
                                            <div class="ys-btn-close" style="top: 15px;left: 172px;">×</div>
                                            <div class="selected-group-goods">
                                                <div class="goods-thumb"><img style="width: 162px;height: 162px" src="{$spec[spec_img]}"/></div>
                                                <div class="goods-name">
                                                    <a target="_blank"
                                                       href="{:U('Home/Goods/goodsInfo',array('id'=>$goods['goods_id'],'item_id'=>$spec['item_id']))}">{$goods.goods_name}{$spec.key_name}</a>
                                                </div>
                                                <div class="goods-price">商城价：￥{$spec.price}库存:{$spec.store_count}</div>
                                            </div>
                                        </div>
                                    </if>
                                </volist>
                            </empty>
                        </volist>
                    </div>
                    <span class="err" id="err_goods" style="color:#F00; display:none;">请添加优惠商品</span>
                    <p class="notic">
                        <a onclick="selectGoods()" class="ncap-btn"><i class="fa fa-search"></i>选择商品</a>
                    </p>
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label>活动描述</label>
                </dt>
                <dd class="opt">
                    <textarea class="span12 ckeditor" placeholder="请输入活动介绍" id="post_content" name="description" rows="6">{$info.description}</textarea>
                    <p class="notic">活动描述</p>
                </dd>
            </dl>
            <if condition="$info['is_edit'] eq 0">
                <div class="bot"><a class="ncap-btn-big">确认提交</a></div>
            <else/>
            <dl class="row" txt="发布活动时，进行通知">
                <dt class="tit">站内信通知</dt>
                <dd class="opt">
                    <div class="onoff">
                        <label for="mmt_message_switch1" class="cb-enable selected">是</label>
                        <label for="mmt_message_switch0" class="cb-disable ">否</label>
                        <input id="mmt_message_switch1" name="mmt_message_switch" checked="checked" value="1" type="radio">
                        <input id="mmt_message_switch0" name="mmt_message_switch" value="0" type="radio">
                    </div>
                    <p class="notic"></p>
                </dd>
            </dl>
                <div class="bot"><a id="submit" class="ncap-btn-big ncap-btn-green">确认提交</a></div>
            </if>
        </div>
    </form>
</div>
<script type="text/javascript">
    $(function () {
        $(document).on("click", '#submit', function (e) {
            var type = parseInt($("#prom_type").val());
            var expression = $("[name='expression']").val();
            if (type == 3 && expression == 0){
                layer.msg('请选择优惠券',{icon:2});return false;
            }
            verifyForm();
        })
    })
    var ajax_return_srtatus=1;
    function verifyForm(){
        if(ajax_return_srtatus==0){
            return false;
        }
        ajax_return_srtatus=0;
        $('span.err').hide();
        $.ajax({
            type: "POST",
            url: "{:U('Admin/Promotion/prom_goods_save')}",
            data: $('#handleposition').serialize(),
            async:false,
            dataType: "json",
            error: function () {
                layer.alert("服务器繁忙, 请联系管理员!");
                ajax_return_srtatus=1
            },
            success: function (data) {
                if (data.status == 1) {
                    layer.msg(data.msg, {icon: 1, time: 1000}, function(){
                        location.href = "{:U('Admin/Promotion/prom_goods_list')}";
                    });
                } else {
                    ajax_return_srtatus=1
                    layer.msg(data.msg, {icon: 2,time: 1000});
                    $.each(data.result, function (index, item) {
                        $('#err_' + index).text(item).show();
                    });
                }
            }
        });
    }
    function call_back(goodsItem){
        var html = '';
        $.each(goodsItem, function (index, item) {
            if (item.goods_id != 'on') {
                if (item.spec != null) {
                    //有规格
                    $.each(item.spec, function (i, o) {
                        html += '<div style="float: left;margin-right: 20px"><div class="ys-btn-close" style="top: 15px;left: 172px;">×</div>' +
                                '<input type="hidden" name="goods[' + item.goods_id + '][goods_id]" value="' + item.goods_id + '"/>' +
                                '<input type="hidden" name="goods[' + item.goods_id + '][item_id][' + i + ']" value="' + o.item_id + '"/>' +
                                '<div class="selected-group-goods"><div class="goods-thumb">' +
                                '<img style="width: 162px;height: 162px" src="' + o.spec_img + '"/></div> <div class="goods-name"> ' +
                                '<a target="_blank" href="/index.php?m=Home&c=Goods&a=goodsInfo&id=' + item.goods_id + '">' + item.goods_name + o.key_name + '</a> </div>' +
                                '<input type="hidden" name="store_count[]" value="'+o.store_count+'"/>' +
                                ' <div class="goods-price">商城价：￥' + o.price + '库存:' + o.store_count + '</div> </div></div>';
                    });
                } else {
                    html += '<div style="float: left;margin-right: 20px"><div class="ys-btn-close" style="top: 15px;left: 172px;">×</div>' +
                            '<input type="hidden" name="goods[' + item.goods_id + '][goods_id]" value="' + item.goods_id + '"/>' +
                            '<div class="selected-group-goods"><div class="goods-thumb">' +
                            '<img style="width: 162px;height: 162px" src="' + item.goods_image + '"/></div> <div class="goods-name"> ' +
                            '<a target="_blank" href="/index.php?m=Home&c=Goods&a=goodsInfo&id=' + item.goods_id + '">' + item.goods_name + '</a> </div>' +
                            '<input type="hidden" name="store_count[]" value="'+item.store_count+'"/>' +
                            ' <div class="goods-price">商城价：￥' + item.goods_price + '库存:' + item.store_count + '</div> </div></div>';
                }
            }
        });
        $('#selected_group_goods').append(html);
        layer.closeAll('iframe');
    }
    var url="{:url('Admin/Ueditor/index',array('savePath'=>'activity'))}";
    var ue = UE.getEditor('post_content',{
        serverUrl :url,
        zIndex: 999,
        initialFrameWidth: "100%", //初化宽度
        initialFrameHeight: 350, //初化高度
        focus: false, //初始化时，是否让编辑器获得焦点true或false
        maximumWords: 99999, removeFormatAttributes: 'class,style,lang,width,height,align,hspace,valign',//允许的最大字符数 'fullscreen',
        pasteplain:false, //是否默认为纯文本粘贴。false为不使用纯文本粘贴，true为使用纯文本粘贴
        autoHeightEnabled: true
    });

    function img_call_back(fileurl_tmp) {
        $("#prom_img").val(fileurl_tmp);
        $("#img_a").attr('href', fileurl_tmp);
        $("#img_i").attr('onmouseover', "layer.tips('<img src="+fileurl_tmp+">',this,{tips: [1, '#fff']});");
    }

    function selectGoods(){
        var url = "{:U('Promotion/search_goods',array('prom_type'=>3,'prom_id'=>$info[id]))}";
        layer.open({
            type: 2,
            title: '选择商品',
            shadeClose: true,
            shade: 0.3,
            area: ['70%', '80%'],
            content: url,
        });
    }

    $("#prom_type").on("change",function(){
        var type = parseInt($("#prom_type").val());
        var coupon_count = $('#coupon_count').val();
        if (type == 3 && coupon_count <= 0){
            layer.msg('没有可选择的优惠券',{icon:2});
        }
        var expression = '';
        switch(type){
            case 0:{
                expression = '<dt class="tit"><label><em>*</em>折扣</label></dt>'
                        + '<dd class="opt"><input type="text" name="expression" pattern="^\\d+$" value="" class="input-txt">'
                        + '<span class="err" id="err_expression"></span><p class="notic">% 折扣值(1-100 如果打9折，请输入90)</p></dd>';
                break;
            }
            case 1:{
                expression = '<dt class="tit"><label><em>*</em>立减金额</label></dt>'
                        + '<dd class="opt"><input type="text" name="expression" pattern="^\\d+(\\.\\d+)?$" value="" class="input-txt">'
                        + '<span class="err" id="err_expression"></span><p class="notic">立减金额（元）</p></dd>';
                break;
            }
            case 2:{
                expression = '<dt class="tit"><label><em>*</em>出售金额</label></dt>'
                        + '<dd class="opt"><input type="text" name="expression" pattern="^\\d+(\\.\\d+)?$" value="" class="input-txt">'
                        + '<span class="err" id="err_expression"></span><p class="notic">出售金额（元）</p></dd>';
                break;
            }
            case 3:{
                expression = '<dt class="tit"><label><em>*</em>代金券</label></dt><dd class="opt"><select name="expression"><option value="0">请选择</option>'
                        + '<foreach name="coupon_list" item="v"><option value="{$v.id}" <if condition="$v[id] eq $info[expression]">selected</if>>{$v.name}</option></foreach></select>'
                        + '<span class="err" id="err_expression"></span><p class="notic">请选择代金券</p></dd>';
                break;
            }
            case 4:{
                expression = '<dt class="tit"><label><em>*</em>买M送N</label></dt>'
                        + '<dd class="opt"><input type="text" name="expression" pattern="\\d+\/\\d+" value="" class="input-txt">'
                        + '<span class="err" id="err_expression"></span><p class="notic">买几件送几件（如买3件送1件: 3/1）</p></dd>';
                break;
            }
        }
        $("#expression").html(expression);
    });

    $(document).ready(function(){
        $("#prom_type").trigger('change');
        $('input[name=expression]').val("{$info.expression}");
        laydate.render({
            elem: '#start_time',//绑定元素
            theme: 'molv', //主题
            type:'datetime', //控件选择类型
            format: 'yyyy-MM-dd HH:mm:ss', //自定义格式
            calendar: true, //显示公历节日
            min: '1970-01-01 00:00:00', //最小日期
            max: '2099-12-31 00:00:00', //最大日期
            // value: new Date(),//默认当前时间
            isInitValue: true,
            position : 'fixed', //定位方式
            zIndex: 99999999, //css z-index
        });
        laydate.render({
            elem: '#end_time',//绑定元素
            theme: 'molv', //主题
            type:'datetime', //控件选择类型
            format: 'yyyy-MM-dd HH:mm:ss', //自定义格式
            calendar: true, //显示公历节日
            min: '1970-01-01 00:00:00', //最小日期
            max: '2099-12-31 00:00:00', //最大日期
            // value: new Date(),//默认当前时间
            isInitValue: true,
            position : 'fixed', //定位方式
            zIndex: 99999999, //css z-index
        });
    })
    //商品删除按钮事件
    $(function () {
        $(document).on("click", '.ys-btn-close', function (e) {
            $(this).parent().remove();
        })
    })
</script>
</body>
</html>